<template>
  <el-card>
    <div>
      <el-row type="flex">
        <el-input
          placeholder="请输入商品名称"
          clearable
          v-model.trim="inputValue"
          style="width: 400px; margin-right: 10px"
        >
          <el-button
            slot="append"
            icon="el-icon-search"
            @click="search"
          ></el-button>
        </el-input>
        <el-button @click="addCommodity" size="small" type="primary"
          >发布商品</el-button
        >
        <el-button
          type="primary"
          icon="el-icon-refresh-right"
          circle
          @click="refresh"
        ></el-button>
      </el-row>
      <el-tabs v-model="tabsValue">
        <el-tab-pane label="在售中" name="1"></el-tab-pane>
        <el-tab-pane label="已下架" name="2"></el-tab-pane>
        <el-tab-pane label="已售罄" name="3"></el-tab-pane>
        <el-tab-pane label="已驳回" name="4"></el-tab-pane>
        <el-tab-pane label="审核中" name="5"></el-tab-pane>
      </el-tabs>
      <el-table
        height="550"
        :data="tableData"
        style="width: 100%; margin: 10px 0"
        border
        :height="autoHeight"
        v-loading.body="tableLoading"
      >
        <!-- <el-table-column type="expand">
          <template slot-scope="props">
          </template>
</el-table-column> -->
        <el-table-column label="商品名称" prop="name" align="center">
        </el-table-column>
        <el-table-column label="商品图片" prop="picUrl" align="center">
          <template slot-scope="scope">
            <el-image
              v-if="scope.row.picUrl"
              :src="$imgUrl(scope.row.picUrl.split(',')[0])"
              fit="fill"
              style="width: 50px; height: 50px"
              :preview-src-list="[$imgUrl(scope.row.picUrl.split(',')[0])]"
            ></el-image>
          </template>
        </el-table-column>
        <el-table-column label="商品优惠价" prop="discountPrice" align="center">
        </el-table-column>
        <el-table-column label="库存" prop="inventoryNum" align="center">
        </el-table-column>
        <el-table-column label="商品累计销量" prop="saleNum" align="center">
        </el-table-column>
        <el-table-column
          label="操作"
          :width="['1', '2', '4'].includes(tabsValue) ? '240' : '160'"
          align="center"
          v-if="tabsValue !== '5'"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              type="primary"
              v-if="tabsValue === '4'"
              @click="reason(scope.row)"
              >驳回原因</el-button
            >
            <el-button
              size="mini"
              type="primary"
              v-if="tabsValue === '1' || tabsValue === '4'"
              @click="editCommodity(scope.row)"
              >编辑</el-button
            >
            <el-button
              size="mini"
              type="success"
              v-if="tabsValue === '2'"
              @click="putOn(scope.row)"
              >上架</el-button
            >
            <el-button
              size="mini"
              type="danger"
              @click="delCommodity(scope.row)"
              >删除</el-button
            >
            <el-button
              size="mini"
              type="warning"
              v-if="tabsValue === '1'"
              @click="pullOff(scope.row)"
              >下架</el-button
            >
          </template>
        </el-table-column>
      </el-table>
    </div>
  </el-card>
</template>
<script>
import {
  getLoginUserGroupCommodityList,
  downCommodity,
  getCommodityById,
  deleteCommodity,
  ShopCommodityCustomizeGroupUnbindCommodity,
} from "@/api/commodity.js";
import { mapMutations } from "vuex";
export default {
  data() {
    return {
      tableData: [],
      tabsValue: "1", // 1 在售中 2 已下架 3 已售罄 4 已驳回 5 审核中
      inputValue: "", // 搜索框
      tableLoading: false,
    };
  },
  created() {
    if (localStorage.getItem("shop")) {
      this.shopId = JSON.parse(localStorage.getItem("shop")).id;
    }
    this.getCommodityList();
  },
  watch: {
    tabsValue() {
      this.tableData = [];
      this.getCommodityList();
    },
    // inputValue(newVal) {
    //   this.tableData = this.tableData.filter(item => {
    //     return item.name.includes(newVal)
    //   })
    // }
  },
  computed: {
    //动态高度
    autoHeight() {
      return window.innerHeight - 235;
    },
  },
  methods: {
    ...mapMutations(["setShopInfo"]),
    refresh() {
      this.tableData = [];
      this.getCommodityList();
    },
    // 查看驳回原因
    reason(row) {
      this.$message({
        message: row.auditResult,
        type: "warning",
        duration: 5000,
      });
    },
    // 搜索
    search() {
      if (this.inputValue === "") {
        this.getCommodityList();
      } else {
        this.tableData = this.tableData.filter((item) => {
          return item.name.includes(this.inputValue);
        });
      }
    },
    getCommodityById(commodityId) {
      return new Promise((resolve, reject) => {
        getCommodityById({ commodityId })
          .then((res) => {
            console.log(res.data);
            resolve(res.data);
          })
          .catch((err) => {
            reject(err);
          });
      });
    },
    // 获取商品列表
    getCommodityList() {
      this.tableLoading = true;
      const shopId = this.shopId;
      getLoginUserGroupCommodityList({
        groupId: null,
        shopId,
        type: +this.tabsValue,
      })
        .then((res) => {
          console.log(res.data, "商品列表");
          if (res.data) {
            this.tableData = res.data;
          }
        })
        .finally(() => {
          this.tableLoading = false;
        });
    },
    // 发布商品
    addCommodity() {
      this.setShopInfo({});
      this.$router.push({
        path: "/commodityPage",
        query: {
          type: "add",
        },
      });
    },
    // 编辑商品
    editCommodity(row) {
      this.getCommodityById(row.id)
        .then((res) => {
          this.setShopInfo({ ...res, id: row.id });
          this.$router.push({
            path: "/commodityPage",
            query: {
              type: "edit",
            },
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 上架
    putOn(row) {
      console.log(row, "row");
      this.getCommodityById(row.id)
        .then((res) => {
          this.setShopInfo({ ...res, id: row.id });
          this.$router.push({
            path: "/commodityPage",
            query: {
              type: "putOn",
            },
          });
        })
        .catch((err) => {
          console.log(err);
        });
    },
    // 下架
    async pullOff(row) {
      const comInfo = await this.getCommodityById(row.id);
      this.$alert("确定要下架这个商品吗?", "温馨提示", {
        showCancelButton: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(async () => {
          await ShopCommodityCustomizeGroupUnbindCommodity({
            commodityIds: [row.id],
            groupId: comInfo.groupList[0],
            shopId: this.shopId,
           });
          downCommodity({ commodityIds: row.id + "" }).then((res) => {
            if (res.code === "200") {
              this.$message.success("下架成功");
              this.getCommodityList();
            }
          });
        })
        .catch(() => {});
    },
    //删除
    delCommodity(row) {
      this.$alert("确定要删除这个商品吗?", "温馨提示", {
        showCancelButton: true,
        confirmButtonText: "确定",
        cancelButtonText: "取消",
      })
        .then(() => {
          deleteCommodity({ id: row.id + "" }).then((res) => {
            if (res.code === "200") {
              this.$message.success("删除成功");
              this.getCommodityList();
            }
          });
        })
        .catch(() => {});
    },
  },
};
</script>

<style lang="less" scoped>
// 给下面这个样式使用vue2的深度选择器
:deep(.el-table .el-table__cell) {
  padding: 10px 0;
}

//自定义滚动条样式
::v-deep .el-table__body-wrapper::-webkit-scrollbar {
  width: 4px;
  height: 4px;
}

::v-deep .el-table__body-wrapper::-webkit-scrollbar-track {
  background-color: #f1f1f1;
  border-radius: 5px;
}

::v-deep .el-table__body-wrapper::-webkit-scrollbar-thumb {
  background-color: #009af2;
  border-radius: 5px;
}
</style>